﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using System.Drawing;
using System.Threading;

namespace PedesTrainProject.Grid
{
    [Serializable()]
    class PedestrianLight : Light, ISerializable
    {
        private ManualTrigger trigger;
        private Directions direction;
        private Directions direction2;
        private Random rand = new Random();
        public List<int> Intervals
        {
            get { return intervals; }
            set { intervals = value; }
        }

        public bool Green
        {
            get { return green; }
            set { green = value; }
        }

        public bool Red
        {
            get { return red; }
            set { red = value; }
        }

        public PedestrianLight()
        {

        }

        public PedestrianLight(List<int> _intervals, Directions _direction): base(_intervals)
        {
            intervals = _intervals;
            direction = _direction;
            green = true;
            red = false;

            if (direction == Directions.South) direction2 = Directions.North;
            if (direction == Directions.North) direction2 = Directions.South;
            if (direction == Directions.East) direction2 = Directions.West;
            if (direction == Directions.West) direction2 = Directions.East;

            trigger = new ManualTrigger(intervals[0], false);
        }

        public PedestrianLight(List<int> _intervals, ManualTrigger _trigger):base(_intervals)
        {

        }

        public PedestrianLight(List<int> _intervals, bool _green, bool _yellow, bool _red)
            : base(_intervals)
        {

        }

        public PedestrianLight(List<int> _intervals, bool _green, bool _yellow, bool _red, ManualTrigger _trigger)
            : base(_intervals)
        {

        }



        public void change(Core.ICoreGUIRun coreGUIRun, int pos)
        {

            while (true)
            {
                
                //if (trigger.Active)
               // {
                //    green = false;
                 //   red = true;
                  //  trigger.Active = false;
                //}
                if (Green == true)
                {
                    coreGUIRun.changeLights(Convert.ToInt32(Core.Calculations.gridPosition(pos).X * 100), Convert.ToInt32(Core.Calculations.gridPosition(pos).Y * 100), Color.Green, direction);
                    coreGUIRun.changeLights(Convert.ToInt32(Core.Calculations.gridPosition(pos).X * 100), Convert.ToInt32(Core.Calculations.gridPosition(pos).Y * 100), Color.Green, direction2);
                    Thread.Sleep(intervals[1]);  
                    Green = true;
                    Red = false;
                   
                }
                if (rand.Next(0, 100) % 20 == 0)
                {
                    Green = false;
                    Red = true;
                }
                else
                {
                    Green = true;
                    Red = false;
                }
                if (Red == true)
                {
                    coreGUIRun.changeLights(Convert.ToInt32(Core.Calculations.gridPosition(pos).X * 100), Convert.ToInt32(Core.Calculations.gridPosition(pos).Y * 100), Color.Red, direction);
                    coreGUIRun.changeLights(Convert.ToInt32(Core.Calculations.gridPosition(pos).X * 100), Convert.ToInt32(Core.Calculations.gridPosition(pos).Y * 100), Color.Red, direction2);
                    Thread.Sleep(intervals[1]);          
                    Green = true;
                    Red = false;
                } 
            }
        }

        #region Serialize
      
        public PedestrianLight(SerializationInfo info, StreamingContext ctxt)
        {
            
            this.Intervals = (List<int>)info.GetValue("Intervals", typeof(List<int>));
            this.name = (string)info.GetValue("Name", typeof(string));
            this.skin = (string)info.GetValue("Skin", typeof(string));
            this.trigger = (ManualTrigger)info.GetValue("Trigger", typeof(ManualTrigger));
            this.direction = (Directions)info.GetValue("Direction", typeof(Directions));
            this.direction2 = (Directions)info.GetValue("Direction2", typeof(Directions));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("Intervals", this.Intervals);
            info.AddValue("Name", this.name);
            info.AddValue("Skin", this.skin);
            info.AddValue("Trigger", this.trigger);
            info.AddValue("Direction", this.direction);
            info.AddValue("Direction2", this.direction2);
        }
        #endregion
    }
}
